package edu.unh.sustain;

import java.util.ArrayList;
import java.util.List;

import org.achartengine.ChartFactory;
import org.achartengine.chart.PointStyle;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;
import org.w3c.dom.Document;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Paint.Align;
import android.util.Log;

public class UtilityChartActivity extends AbstractDemoChart {
	private String id;
	private String span;
	private String name;
	private int day;
	private int month;
	private int year;
	private EnergyReading er;
	
	public void setBuildingId(String i)
	{
		id = i;
	}
	
	public void setName(String n)
	{
		name = n;
	}

	public void setDate(int y, int m, int d, String s)
	{
		day = d;
		month = m;
		year = y;
		span = s;
	}
	
	public Intent execute(Context context) {
		List<Integer> colors = new ArrayList<Integer>();
		List<String> titles = new ArrayList<String>();
		Document doc = XMLfunctions.chartXMLfromWeb(id, span, year, month, day);
		
		er = new EnergyReading(doc, span, month);
		
		if(er.hasElectric())
		{
			titles.add("Electric (kW/h)");
			colors.add(new Integer(Color.rgb(245, 184, 0)));
			Log.w("added title", "elec");
		}
		
		if(er.hasWater())
		{
			titles.add("Water (gal)");
			colors.add(new Integer(Color.rgb(0, 164, 219)));
			Log.w("added title", "water");
		}
		
		if(er.hasGas())
		{
			titles.add("Gas (cu. ft)");
			colors.add(new Integer(Color.rgb(0, 245, 61)));
			Log.w("added title", "gas");
		}
		
		List<Double[]> values = er.getValues();
		
		// create renderer
		XYMultipleSeriesRenderer renderer = buildRenderer(colors);
		int length = renderer.getSeriesRendererCount();
		for (int i = 0; i < length; i++) {
			((XYSeriesRenderer) renderer.getSeriesRendererAt(i)).setFillPoints(false);
			((XYSeriesRenderer) renderer.getSeriesRendererAt(i)).setLineWidth(5.0f);
		}

		// prefix for title
		StringBuilder prefix = new StringBuilder();
		if(er.isDay())
		{
			prefix.append("The day of ");
		} else if(er.isWeek()) {
			prefix.append("The week of ");
		} else if(er.isMonth()) {
			prefix.append("The month of ");
		} else if(er.isYear()) {
			prefix.append("The year of ");
		}
		prefix.append(er.getPDate());
		
		int denom;
		if(er.getDates().get(0).length == 7)
			denom = 1;
		else
			denom = 2;
		
		// title is set here, as well as axis labels
		setChartSettings(renderer, prefix.toString(), "", "", 1, er.getDates().get(0).length, 0, er.getHighVal(),
				Color.LTGRAY, Color.LTGRAY);
		renderer.setXLabels(er.getDates().get(0).length / denom);
		renderer.setYLabels(10);
		renderer.setShowGrid(true);
		renderer.setXLabelsAlign(Align.RIGHT);
		renderer.setYLabelsAlign(Align.RIGHT);
		renderer.setZoomButtonsVisible(false);
		renderer.setPanEnabled(false, false);
		renderer.setZoomEnabled(false, false);
		Log.w("dates size", Integer.toString(er.getDates().get(0).length));
		Intent intent = ChartFactory.getCubicLineChartIntent(context, buildDataset(titles, er.getDates(), values),
				renderer, 0.33f, name);
		
		return intent;
	}

}
